# -*- coding: utf-8 -*-

import hashlib
from base import UserCase, Runner, TestSuite


class Case(UserCase):

    def create_corp(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(8),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        self.assertEqual(res['code'], 0)
        corp = res['data']
        return corp, p['username'], p['password']

    def login_corp(self, username, password):

        res = self.c.open('/login', {'username': username, 'password': password})
        self.assertEqual(res['code'], 0)

    def create_user(self):
        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        return res['data']['user_obj']

    def create_device(self, corp):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        return res['data']


    def test_my_corp_user_permission(self):
        corp, username, password = self.create_corp()
        self.login_corp(username, password)
        p = {
            'name': self.randhan(3),
            'role': 'normal',
            'password': hashlib.md5(self.randchar(6)).hexdigest(),
            'username': self.randchar(8),
        }
        res = self.c.open('/api/my-corp-user/create', p)
        self.assertEqual(res['code'], 0)

        res = self.c.open('/login', {'username': p['username'], 'password': p['password']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user-device/list', {})
        self.assertNotEqual(res['code'], 0)


    def test_my_corp_user_device_create(self):
        corp, username, password = self.create_corp()
        device = self.create_device(corp['id'])
        self.login_corp(username, password)
        user = self.create_user()

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 0)

        res = self.c.open('/api/my-corp-user-device/create', {'device': device['id'], 'user': user['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 1)


    def test_my_corp_user_device_list(self):
        corp, username, password = self.create_corp()
        device = self.create_device(corp['id'])
        self.login_corp(username, password)
        user = self.create_user()

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['code'], 0)
        self.assertEqual(res['data']['count'], 0)


    def test_my_corp_user_device_delete(self):
        corp, username, password = self.create_corp()
        device = self.create_device(corp['id'])
        self.login_corp(username, password)
        user = self.create_user()

        res = self.c.open('/api/my-corp-user-device/create', {'device': device['id'], 'user': user['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['code'], 0)

        self.assertEqual(res['data']['count'], 1)

        self.assertEqual(res['data']['itemList'][0]['device_obj']['id'], device['id'])

        res = self.c.open('/api/my-corp-user-device/delete', {'user': user['id'], 'device': device['id']})
        self.assertEqual(res['code'], 0)

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 0)


    def test_my_corp_user_device_list_filter(self):
        corp, username, password = self.create_corp()



        device_a = self.create_device(corp['id'])
        device_b = self.create_device(corp['id'])
        device_c = self.create_device(corp['id'])

        self.login_corp(username, password)
        user_a = self.create_user()
        user = self.create_user()

        self.c.open('/api/my-corp-user-device/create', {'device': device_a['id'], 'user': user_a['id']})
        self.c.open('/api/my-corp-user-device/create', {'device': device_a['id'], 'user': user['id']})
        self.c.open('/api/my-corp-user-device/create', {'device': device_b['id'], 'user': user['id']})
        self.c.open('/api/my-corp-user-device/create', {'device': device_c['id'], 'user': user['id']})

        res = self.c.open('/api/my-corp-user-device/list')
        self.assertEqual(res['data']['count'] > 3, True)

        res = self.c.open('/api/my-corp-user-device/list', {'user': user['id']})
        self.assertEqual(res['data']['count'], 3)



test_list = [
    'test_my_corp_user_permission',
    'test_my_corp_user_device_create',
    'test_my_corp_user_device_list',
    'test_my_corp_user_device_delete',
    'test_my_corp_user_device_list_filter',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



